package svgedit.gui.command;

import java.util.Stack;

import svgedit.gui.View;
import svgedit.svg.SVGDocument;
import svgedit.svg.SVGElement;

/**
 *  Define insert execution as a command object which has 
 *      execute (redo) and undo methods
 * */
public class InsertCommand implements Command{

	private View view;
	private SVGElement insertElement;
	private SVGElement element;
	private SVGDocument document;
	
	/** Constructor for insert command
	 * 
	 * @param view to get the view panel
	 * @param element to get the element which is inserted
	 * */
	public InsertCommand(View view, SVGElement element){
		this.view = view;
		this.insertElement = element;
		document = view.getDocument();
	}
	
	/** 
	 *  When execute to insert an element or when redo to insert an element,
	 *  add this element into the document
	 *  */
	public void execute(){
        document.getRootGroup().appendChild(insertElement);
        element = insertElement;
        document.setModified(true);
        view.setSelectedElement(insertElement);
        view.setSelectionManipulator();
        insertElement = null;
	}
	
	/** 
	 *  When insert an element, undo it will delete this element
	 * */
	public void undo(){
		insertElement = element;
		view.clearSelection();
		document.getRootGroup().removeChild(insertElement);
		view.repaint();
	}
}
